﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Xml.Linq;

using ProjectBase.Core;
using ProjectBase.Data;
using ProjectBase.Data.Xml;

using Survey.Common;
using Survey.Common.Data;
using Survey.Entity;

namespace Survey.Data.Xml
{
    public class ContentTypeDao : BaseXmlDao<ContentType, int, ContentTypeFilter>, IContentTypeDao
    {
        #region Find
        /// <summary>
        ///     Fills the Criteria list with where statements based on provided filter
        /// </summary>
        /// <param name = "filter"></param>
        /// <returns></returns>
        public override IEnumerable<ContentType> Find(ContentTypeFilter filter)
        {
            // Equal
            Criteria.Add(entity => entity.ID.IsEqual(filter.IdExact));
            Criteria.Add(entity => entity.Code.IsExact(filter.CodeExact)); // exact match even for string.Empty

            // Like
            Criteria.Add(entity => entity.Code.IsLike(filter.Code));
            Criteria.Add(entity => entity.Name.IsLike(filter.Name));
            Criteria.Add(entity => entity.Suffix.IsLike(filter.Suffix));

            Criteria.Add(entity => entity.IsVisible.IsEqual(filter.IsVisible));

            return GetListByCriteria(filter);
        }
        #endregion Find

        #region XML - ENTITY mapping
        public IEnumerable<ContentType> GetAll()
        {
            return Find(new ContentTypeFilter { MaxRowsPerPage = int.MaxValue });
        }

        /// <summary>
        ///     Converts the XElement into the ContentType
        /// </summary>
        /// <param name = "element"></param>
        /// <returns></returns>
        protected override ContentType CreateEntity(XElement element)
        {
            var entity = FillEntity(new ContentType(), element); // base converts ValueTypes and String

            entity.SetId((int)element.Attribute(Str.Common.ID));
            return entity;
        }

        /// <summary>
        ///     Converts the ContentType instnace into the XElement
        /// </summary>
        /// <param name = "entity"></param>
        /// <returns></returns>
        protected override XElement CreateElement(ContentType entity)
        {
            var element = base.CreateElement(entity); // base converts ValueTypes and String

            return element;
        }
        #endregion XML - ENTITY mapping

        #region NextId
        /// <summary>
        ///     Incremented 'Next ID' generator.
        ///     Searches for the max existing used value
        ///     and returnes that + 1
        /// </summary>
        /// <returns></returns>
        protected override int GetNextId(ContentType entity = null)
        {
            var id = 0; // in case there is no element yet
            if (TheList.Any()) // element(s) exist(s) - find the largest ID
            {
                id = TheList.Max<ContentType, int>(e => e.ID);
            }
            return ++id;
        }
        #endregion NextId
    }
}